package cn.lj.login.controller.controller;

import cn.lj.common.enums.RestResult;
import cn.lj.common.util.BeanCopierUtil;
import cn.lj.dict.mapper.pojo.SmallMiDict;
import cn.lj.dict.service.bo.SmallMiDictBO;
import cn.lj.dict.service.dos.SmallMiDictDO;
import cn.lj.dict.service.service.DictService;
import cn.lj.login.controller.dto.SmallMiForgetPasswordDTO;
import cn.lj.login.controller.dto.SmallMiLoginNumberDTO;
import cn.lj.login.controller.dto.SmallMiUserLoginDTO;
import cn.lj.login.controller.vo.SmallMiUserLoginVO;
import cn.lj.login.service.bo.SmallMiForgetPasswordBO;
import cn.lj.login.service.bo.SmallMiLoginNumberBO;
import cn.lj.login.service.bo.SmallMiUserLoginBO;
import cn.lj.login.service.dos.SmallMiUserLoginDO;
import cn.lj.login.service.service.LoginService;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Description: LoginController
 * @author: lijie
 * @date: 2021年07月16日 下午 12:27:45
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 功能描述: 正常账号密码登录成功或失败接口
     *
     * @param smallMiUserLoginDTO: 传入id，邮箱，手机号和密码
     * @auther: lijie
     * @date: 2021/7/16 下午 03:00:51
     * @return: cn.lj.common.enums.RestResult<java.lang.Void>
     */
    @PostMapping("/inlogin")
    public RestResult<Void> inlogin(SmallMiUserLoginDTO smallMiUserLoginDTO) {
        SmallMiUserLoginBO smallMiUserLoginBO = BeanCopierUtil.copyObj(smallMiUserLoginDTO, SmallMiUserLoginBO.class);
        RestResult<Void> inlogin = loginService.inlogin(smallMiUserLoginBO);
        return inlogin;
    }

    /**
     * 功能描述: 获取验证码
     *
     * @param number: 传入手机号
     * @auther: lijie
     * @date: 2021/7/16 下午 05:55:28
     * @return: cn.lj.common.enums.RestResult<java.lang.Void>
     */
    @GetMapping("/inloginyzm")
    public RestResult<Void> inloginyzm(String number) throws Exception {
        RestResult<Void> restResult = loginService.inloginyzm(number);
        return restResult;
    }

    /**
     * 功能描述: 使用手机号验证码进行登录接口
     *
     * @param smallMiLoginNumberDTO: 传入手机号phone，验证码yzm
     * @auther: lijie
     * @date: 2021/7/16 下午 07:35:40
     * @return: cn.lj.common.enums.RestResult<java.lang.Void>
     */
    @PostMapping("/inloginnumber")
    public RestResult<Void> inloginnumber(SmallMiLoginNumberDTO smallMiLoginNumberDTO) {
        SmallMiLoginNumberBO smallMiLoginNumberBO = BeanCopierUtil.copyObj(smallMiLoginNumberDTO, SmallMiLoginNumberBO.class);
        RestResult<Void> restResult = loginService.inloginnumber(smallMiLoginNumberBO);
        return restResult;
    }

    /**
     * 功能描述: 找回密码接口
     *
     * @param smallMiForgetPasswordDTO: 传入手机号phone，验证码yzm，密码password
     * @auther: lijie
     * @date: 2021/7/17 下午 03:05:20
     * @return: cn.lj.common.enums.RestResult<java.lang.Void>
     */
    @PostMapping("/ForgetPassword")
    public RestResult<Void> ForgetPassword(SmallMiForgetPasswordDTO smallMiForgetPasswordDTO) throws Exception {
        SmallMiForgetPasswordBO smallMiForgetPasswordBO = BeanCopierUtil.copyObj(smallMiForgetPasswordDTO, SmallMiForgetPasswordBO.class);
        RestResult<Void> restResult = loginService.ForgetPassword(smallMiForgetPasswordBO);
        return restResult;
    }

    /**
     * 功能描述:第三方qq登录
     *
     * @param request:
     * @param response:
     * @auther: lijie
     * @date: 2021/7/24 上午 09:04:51
     * @return: void
     */
    @GetMapping("/qqLogin")
    public void qqLogin(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (QQConnectException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录回调
     */
    /*@GetMapping("/huidiao")
    public String qqCallBack(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        try {
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            String accessToken = null, openID = null;
            long tokenExpireIn = 0L;
            if (accessTokenObj.getAccessToken().equals("")) {
                return "404";
            }
            accessToken = accessTokenObj.getAccessToken();
            tokenExpireIn = accessTokenObj.getExpireIn();
            // 利用获取到的accessToken 去获取当前用的openid
            OpenID openIDObj = new OpenID(accessToken);
            openID = openIDObj.getUserOpenID();
            String icon = null, nickName = null, sex = null;
            // 去获取用户在Qzone的昵称等信息
            com.qq.connect.api.qzone.UserInfo qzoneUserInfo =
                    new com.qq.connect.api.qzone.UserInfo(accessToken, openID);
            UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
            if (userInfoBean.getRet() == 0) {
                nickName = userInfoBean.getNickname();
                sex = userInfoBean.getGender();
                if (userInfoBean.getAvatar().getAvatarURL100() == null) {
                    icon = userInfoBean.getAvatar().getAvatarURL50();
                }
                icon = userInfoBean.getAvatar().getAvatarURL100();
                // userInfoBean.getAvatar().getAvatarURL50();
                // userInfoBean.getAvatar().getAvatarURL100();
            }
            // 自己网站的用户实体类，根据openId查询是否已存在
            UserInfo userInfo = new UserInfo();
            userInfo.setOpenId(openID);
            UserInfo localUser = userInfoService.selectByOpenId(userInfo);
            if (localUser != null) {
                // 老用户处理
                //你需要的业务操作
            } else {
                // 新用户处理
                UserInfo userInfos = new UserInfo();
                userInfos.setUserId(Tools.getUniqueId());
                userInfos.setUserName(nickName);
                userInfos.setSex(sex);
                userInfoService.save(userInfos);
                //你需要的业务操作
            }
        } catch (QQConnectException e) {
            e.printStackTrace();
        }
        return "redirect:/index";
    }*/
}
